package GUI;//done

import java.awt.Rectangle;
import java.awt.SystemColor;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Client.ConnectionToServer;
import Common.Settings;

/**
 * This Class controls DealSale Search Window.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class DealSaleSearchWindow extends JFrame{
	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;

	/** The Template ID JComboBox. */
	private JComboBox tempIDComboBox = null;
	
	/** The Template ID Label. */
	private JLabel tempIDlLabel = null;
	
	/** The Search Button. */
	private JButton searchButton = null;
	
	/** The Content Pane. */
	private JPanel jContentPane = null;
	
	/** The Template ID. */
	private long tempID = 0;
	
	/** The Report Window. */
	private ReportWindow reportWindow = null;
	
	/** The Background Label. */
	private JLabel BackgroundLabel;
	
	/**
	 * The default constructor
	 */
	public DealSaleSearchWindow(){
		super();
		getContentPane().setBackground(SystemColor.activeCaption);
		initialize();		
	}
	
	/**
	 * This function initializes DealSaleSearchWindow
	 */
	private void initialize(){
		this.setContentPane(getJContentPane());
		this.setTitle("Deal Sale SEARCH");
		
		BackgroundLabel = new JLabel("");
		BackgroundLabel.setIcon(new ImageIcon(DealSaleSearchWindow.class.getResource("/Pictures/bg.png")));
		BackgroundLabel.setBounds(0, 0, 300, 300);
		getContentPane().add(BackgroundLabel);		
		
	}
	
	/**
	 * This function initializes JContentPane
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			
			tempIDlLabel = new JLabel();
			tempIDlLabel.setBounds(new Rectangle(15, 17, 113, 22));
			tempIDlLabel.setText("Deal Sale number:");
							
			jContentPane.setLayout(null);
			jContentPane.add(tempIDlLabel, null);
			jContentPane.add(getTempIDComboBox(), null);
			jContentPane.add(getSearchButton(),null);			
		}
		setBounds(Settings.Screen_x, Settings.Screen_y, 673, 488);
		return jContentPane;		
	}
	
	/**
	 * This function initializes PIDComboBox	
	 */
	private JComboBox getTempIDComboBox(){
		if(tempIDComboBox == null){	
			tempIDComboBox = new JComboBox();
			tempIDComboBox.setBounds(new Rectangle(133, 17, 127, 25));
			long[] templateID = null;
			try{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				templateID = (long[]) connectiontoserver.loadAllDealSales();
				if (templateID == null){
					JOptionPane.showMessageDialog(null, "No Templates available!");
					tempIDComboBox.addItem(0);
				}	
				else{
					int len = templateID.length;
					if (len > 0)
						tempID = templateID[0];
					for(int i=0; i < len; i++){
						tempIDComboBox.addItem(templateID[i]);
					}	
				}
			}
			catch(Exception e){
				tempIDComboBox.addItem(0);
			}
			tempIDComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					tempID = (Long)tempIDComboBox.getSelectedItem();	
				}	
			});	
		}	
		return tempIDComboBox;
	}

	
	/**
	 * This function initializes searchButton	
	 */
	private JButton getSearchButton() {
		if (searchButton == null) {
			searchButton = new JButton();
			searchButton.setBounds(new Rectangle(90, 70, 130, 29));
			searchButton.setText("Search");
			searchButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try{
						if (tempID > 0){
							ConnectionToServer connectionToServer = new ConnectionToServer();
							String msg = (String)connectionToServer.getResponseReport(tempID);
							if(msg != null){	
								reportWindow = new ReportWindow();
								JFrame frame = new JFrame("Reports"); 
								frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
								frame.getContentPane().add(reportWindow); 
								frame.pack();
								frame.setSize(300, 300);
								reportWindow.setText(msg);	
								frame.setVisible(true);													
							}	
							else
								JOptionPane.showMessageDialog(null, "CAMPAIGN NOT EXIST IN DATA BASE","",JOptionPane.WARNING_MESSAGE);
						}
						else
							JOptionPane.showMessageDialog(null, "PLZ select a campaign");
					} 
					catch (Exception excp){
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);	
					}											
				}	
			});
		}
		return searchButton;
	}
}
